from django.db import models
from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericForeignKey
from courese.fields import OrderField
# Create your models here.


class Subject(models.Model):
    title = models.CharField(max_length=200, verbose_name='标题')
    slug = models.SlugField(max_length=200, unique=True, verbose_name='标签')

    class Meta:
        verbose_name = "课程主题"
        verbose_name_plural = verbose_name
        ordering = ('title',)

    def __str__(self):
        return self.title


class Course(models.Model):
    owner = models.ForeignKey(User,
                              related_name='courses_created',
                              verbose_name='教师',
                              on_delete=models.CASCADE
                              )
    subject = models.ForeignKey(Subject,
                                related_name='courses',
                                verbose_name='课程主题',
                                on_delete=models.CASCADE)
    title = models.CharField(max_length=200,
                             verbose_name='课程标题')
    slug = models.SlugField(max_length=200, unique=True)
    overview = models.TextField(verbose_name='课程概述')
    created = models.DateTimeField(auto_now_add=True,
                                   verbose_name='创建时间')
    students = models.ManyToManyField(User,
                                      related_name='courses_joined',
                                      blank=True)


    class Meta:
        verbose_name = "课程"
        verbose_name_plural = verbose_name
        ordering = ('-created',)

    def __str__(self):
        return self.title


class Module(models.Model):
    course = models.ForeignKey(Course,
                               related_name='modules',
                               on_delete=models.CASCADE,
                               verbose_name='课程')
    title = models.CharField(max_length=200,
                             verbose_name='标题')
    description = models.TextField(blank=True,
                                   verbose_name='描述')
    order = OrderField(blank=True,
                       for_fields=['course'],
                       verbose_name='次序')

    class Meta:
        verbose_name = "模型"
        verbose_name_plural = verbose_name
        ordering = ['order']

    def __str__(self):
        return '{}. {}'.format(self.order, self.title)


class Content(models.Model):
    module = models.ForeignKey(Module,
                               related_name='contents',
                               on_delete=models.CASCADE)
    content_type = models.ForeignKey(ContentType,
                                     on_delete=models.CASCADE,
                                     limit_choices_to={'model__in': ('text',
                                                                     'video',
                                                                     'image',
                                                                     'file')})
    object_id = models.PositiveIntegerField()
    item = GenericForeignKey('content_type', 'object_id')
    order = OrderField(blank=True, for_fields=['module'])

    class Meta:
        ordering = ['order']


class ItemBase(models.Model):
    owner = models.ForeignKey(User,
                              related_name='%(class)s_related',
                              on_delete=models.CASCADE,
                              verbose_name='用户')
    title = models.CharField(max_length=250,
                             verbose_name='标题')
    created = models.DateTimeField(auto_now_add=True,
                                   verbose_name='创建时间')
    updated = models.DateTimeField(auto_now=True,
                                   verbose_name='更新时间')

    class Meta:
        abstract = True

    def __str__(self):
        return self.title

    """
    定义了一个抽象模型命名为ItemBase。除此以外，
    我们在Meta类中设置abstract=True。
    """


class Text(ItemBase):
    content = models.TextField(verbose_name='内容')


class File(ItemBase):
    file = models.FileField(upload_to='files',
                            verbose_name='文件')


class Image(ItemBase):
    file = models.FileField(upload_to='images',
                            verbose_name='图片')


class Video(ItemBase):
    url = models.URLField(verbose_name='视频链接')


from django.template.loader import render_to_string
from django.utils.safestring import mark_safe
class ItemBase(models.Model):
    def render(self):
        return render_to_string('courses/content/{}.html'.format(self._meta.model_name),{"item": self})











